package com.leetcode.No0870;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @program: Solution
 * @description: 优势洗牌
 * 用题解给的方法做
 * @author: Wang Zhihua
 * @date: 2022-10-08
 */
public class Solution2 {
    public int[] advantageCount(int[] nums1, int[] nums2) {

        int len = nums1.length;
        Integer[] idx1 = new Integer[len];
        Integer[] idx2 = new Integer[len];
        for (int i = 0; i < len; ++i) {
            idx1[i] = i;
            idx2[i] = i;
        }

        Arrays.sort(idx1, Comparator.comparingInt(x -> nums1[x]));
        Arrays.sort(idx2, Comparator.comparingInt(x -> nums2[x]));

        int[] ans = new int[len];
        int left = 0, right = len - 1;
        for (int i = 0; i < len; ++i) {
            if (nums1[idx1[i]] > nums2[idx2[left]]) {
                ans[idx2[left++]] = nums1[idx1[i]];
            } else {
                ans[idx2[right--]] = nums1[idx1[i]];
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        Solution2 solution = new Solution2();

        int[] nums1 = {2,7,11,15};
        int[] nums2 = {1,10,4,11};
        System.out.println("nums1: " + Arrays.toString(solution.advantageCount(nums1, nums2)));
        System.out.println("nums2: " + Arrays.toString(nums2));
        System.out.println();

        nums1 = new int[]{12,24,8,32};
        nums2 = new int[]{13,25,32,11};
        System.out.println("nums1: " + Arrays.toString(solution.advantageCount(nums1, nums2)));
        System.out.println("nums2: " + Arrays.toString(nums2));
        System.out.println();

        nums1 = new int[]{718967141,189971378,341560426,23521218,339517772};
        nums2 = new int[]{967482459,978798455,744530040,3454610,940238504};
        System.out.println("nums1: " + Arrays.toString(solution.advantageCount(nums1, nums2)));
        System.out.println("nums2: " + Arrays.toString(nums2));
        System.out.println();
    }
}
